﻿
var pageSize = 20;




$(function () {
    $(document).attr("title", "标签");
    $("#btnAdd").click(function () { Add(); });
    $("#btnDelete").click(function () { DeleteMulti(); });
    $("#btnSave").click(function () { Save(); });
    $("#btnEdit").click(function () { Edit(this); });

    $("#checkAll").click(function () { CheckAll(this) });

    //$('#simple-colorpicker-1').colorpicker();

    $('#MarkColor').ace_colorpicker();

    LoadTables(1, pageSize);
});

//加载列表数据
function LoadTables(startPage, pageSize) {
    $("#tableBody").html("");
    $("#checkAll").prop("checked", false);
    AjaxData("/BookMark/GetAllPageList?startPage=" + startPage + " &pageSize=" + pageSize + "&_t=" + new Date().getTime(), "", "Get", "", function (d) {
        if (d.success) {
            var data = d.data;
            if (data != null && data.result != null) {
                $.each(data.result, function (i, item) {
                    var tr = "<tr>";
                    tr += "<td align='center'><input type='checkbox' class='checkboxs' value='" + item.id + "' /></td>";
                    tr += "<td>" + (i + 1) + "</td>";
                    tr += "<td>" + item.markName + "</td>";
                    tr += "<td>" + item.markDesc + "</td>";
                    tr += "<td>" + item.markIndex + "</td>";
                    tr += "<td><span style=\"width:30px;height:20px;display:block;background-color:" + item.markColor + "\" ></span></td>";
                    tr += "<td>" + item.createAppUserName + "</td>";
                    tr += "<td>" + item.createDate + "</td>";
                    tr += "</tr>";
                    $("#tableBody").append(tr);
                })

            }


            $("#grid_paging_part").lagerPaper({
                currentIndex: data.pageIndex,
                pageSize: pageSize,
                totalCount: data.totalCount,
                pageCount: data.pageCount,
                onPageChanged: function (e, pageIndex) {
                    LoadTables(pageIndex, pageSize);
                }
            });
        }

    });

}
//全选
function CheckAll(obj) {
    $(".checkboxs").each(function () {
        if (obj.checked == true) {
            $(this).prop("checked", true)

        }
        if (obj.checked == false) {
            $(this).prop("checked", false)
        }
    });
};
//新增
function Add() {

    var dialog = $("#editModal").removeClass('hide').dialog({
        modal: true,
        title: '新增标签',
        title_html: false,
        width: 650,
        height: 460

    });

    $("#MarkId").val(-1);
    $("#MarkName").val("");
    $("#MarkDesc").val("");
    $("#MarkIndex").val("");
    $("#MarkColor").val("#ac725e");


};
//编辑
function Edit(selector) {

    var dialog = $("#editModal").removeClass('hide').dialog({
        modal: true,
        title: '编辑标签',
        title_html: false,
        width: 650,
        height: 460

    });

    var ids = [];
    $(".checkboxs").each(function () {
        if ($(this).prop("checked") == true) {
            ids.push($(this).val());
        }
    });
    if (ids.length == 0) {
        layer.tips("请选择要编辑的记录", selector);
        return;
    };
    if (ids.length > 1) {
        layer.tips("不能同时编辑多条记录", selector);
        return;
    }

    $.ajax({
        type: "Get",
        url: "/BookMark/Get?id=" + ids[0] + "&_t=" + new Date().getTime(),
        success: function (d) {
            var data = d.data;
            $("#MarkId").val(data.id);
            $("#MarkName").val(data.markName);
            $("#MarkDesc").val(data.markDesc);
            $("#MarkIndex").val(data.markIndex);
            if (data.markColor == null || data.markColor == "") {
                $("#MarkColor").val("#ac725e");
                
            } else {
                $("#MarkColor").val(data.markColor);
                
                $("#MarkColor").change();
                $(".dropdown-menu").children().each(function () {
                    var color = $(this).children().attr("data-color");
                    if (color == data.markColor) {
                        $(this).children().click();
                    }
                });
            }
            
           
        }
    })
};

//保存
function Save() {
    var postData = {
        "dto":
        {
            "Id": $("#MarkId").val(),
            "MarkName": $("#MarkName").val(),
            "MarkDesc": $("#MarkDesc").val(),
            "MarkIndex": $("#MarkIndex").val(),
            "MarkColor": $("#MarkColor").val()
        }
    };
    $.ajax({
        type: "Post",
        url: "/BookMark/Edit",
        data: postData,
        success: function (data) {
            if (data.success) {

                $(".ui-dialog-titlebar-close").trigger("click");
                LoadTables(1, pageSize);
            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};

//批量删除
function DeleteMulti() {
    var ids = "";
    $(".checkboxs").each(function () {
        if ($(this).prop("checked") == true) {
            ids += $(this).val() + ","
        }
    });
    ids = ids.substring(0, ids.length - 1);
    if (ids.length == 0) {
        layer.alert("请选择要删除的记录。");
        return;
    };
    //询问框
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        var sendData = { "ids": ids };
        $.ajax({
            type: "Post",
            url: "/BookMark/DeleteMuti",
            data: sendData,
            success: function (data) {
                if (data.success) {
                    LoadTables(1, pageSize);
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！" + data.message);
                }
            }
        });
    });
};
